package com.qiuzhiyi.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.li.utils.TransformUtil;
import com.li.widget.TopNavigationBar;
import com.qiuzhiyi.R;
import com.qiuzhiyi.adapter.ShopAdapter;
import com.qiuzhiyi.base.QZYBaseActivity;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.item.ShopItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;

import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Author: Li
 * Date: 2017/11/14 0014
 * Description: 门店界面
 */

public class ShopActivity extends QZYBaseActivity {

    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler)
    RecyclerView mRecycler;

    private ShopAdapter mShopAdapter;
    private List<ShopItem> mShopItemList;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mShopAdapter = new ShopAdapter(R.layout.item_shop);

        ImageView imageView = new ImageView(mContext);
        imageView.setLayoutParams(new RecyclerView.LayoutParams(-1,
                TransformUtil.dp2px(mContext, 160)));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.drawable.zhaogong_head);
        mShopAdapter.setHeaderView(imageView);
        mShopAdapter.bindToRecyclerView(mRecycler);
        mShopAdapter.setEmptyView(R.layout.layout_empty_shop);
        mShopAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String sId= String.valueOf(mShopItemList.get(position).getId());
                Intent intent=new Intent(mContext,StoreDetailActivity.class);
                intent.putExtra("sId",sId);
                startActivity(intent);
            }
        });
        getShopList();
    }

    /**
     * 获取门店列表
     */
    private void getShopList() {
        ObservableHelper.createObservable(RetrofitHelper.createService().getShopList())
                .subscribe(new SelectorObserver<BaseItem<List<ShopItem>>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(BaseItem<List<ShopItem>> item) {
                        mShopItemList=item.getObjValue();
                        mShopAdapter.setNewData(item.getObjValue());
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        showLoadingDialog();
                    }
                });
    }
}
